package com.czk.zookeeper.zookeepercli.watcher.exsit;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/1/4 20:24
 */
public class ZkWatcherExistsTest {
    String IP = "192.168.25.133:2181";
    ZooKeeper zooKeeper = null;

    @Before
    public void before() throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(IP, 100000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("连接对象的参数");
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    // 连接成功
                    countDownLatch.countDown();
                }
                System.out.println("path=" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        // 连接是异步的，此时应该阻塞
        countDownLatch.await();
    }

    @After
    public void after() throws Exception {
        zooKeeper.close();
    }

    @Test
    public void test() throws Exception {
        // 需要进行事件监听
        // arg1 节点路径
        // arg2 使用连接对象中的watcher进行监听
        zooKeeper.exists("/create", true);
        TimeUnit.SECONDS.sleep(100);
        System.out.println("结束");
    }

    /**
     * 自定义watcher
     *
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
        // 自定义watcher
        zooKeeper.exists("/create", new Watcher() {
            // 注意只能监听一次，监听一次后需要重新注册
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher");
                System.out.println("path" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(100000);
        System.out.println("结束");
    }

    /**
     * 持续监听节点
     *
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {
        zooKeeper.exists("/create", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("自定义watcher");
                    System.out.println("path" + watchedEvent.getPath());
                    System.out.println("eventType=" + watchedEvent.getType());
                    // 持续监听，再进行注册
                    zooKeeper.exists("/create", this);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        System.in.read();
    }

    /**
     * 一个几点注册多个监听器对象
     *
     * @throws Exception
     */
    @Test
    public void test4() throws Exception {
        // 注册多个监听器对象
        zooKeeper.exists("/create", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher,监听器对象1");
                System.out.println("path" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        zooKeeper.exists("/create", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("自定义watcher,监听器对象2");
                System.out.println("path" + watchedEvent.getPath());
                System.out.println("eventType=" + watchedEvent.getType());
            }
        });
        Thread.sleep(10 * 1000);
        System.out.println("测试结束");
    }
}
